/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年1月30日 下午3:02:56
 */
package com.lenovo.lmrp.server.itsmgateway.spi;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.lenovo.lmrp.base.GuiceRestServer;
import com.lenovo.lmrp.base.ParamNameDef;
import com.lenovo.sdc.framework.util.AttributeBean;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class ItsmGatewayRestServer {
    private GuiceRestServer restServer;
    private AttributeBean params;
    
    @Inject
    private Injector injector;

    @Inject
    public ItsmGatewayRestServer(AttributeBean params) {
        this.params = params;
        restServer = new GuiceRestServer(
            params.getAttribute(ParamNameDef.REST_SERVER_HOST, "0.0.0.0"), 
            params.getAttribute(ParamNameDef.REST_SERVER_PORT, 7003), 
            params.getAttribute(ParamNameDef.REST_SERVER_SSL, false));
    }

    public void start() {
        // start REST API server, listening special port to receive income request
        restServer.start();
        
        String[] scanpaths = new String[]{params.getAttribute(ParamNameDef.REST_API_SCANPATH, 
                                              "com.lenovo.lmrp.server.itsmgateway.spi"),
                                          "com.lenovo.sdc.framework.rest"};
        
        Injector child = injector.createChildInjector(restServer.bind(scanpaths));
        
        // deploy REST service under GUICE env. to process request
        restServer.deploy(params.getAttribute(ParamNameDef.REST_SERVER_CONTEXT, ""), child.getBindings().values());
    }

    public void stop() {
        restServer.stop();
    }
}
